package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;

import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.MessageAdapter;
import com.yanq.liancar.bean.MessageModel;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.net.util.ProgressSubscriber;
import com.yanq.liancar.util.Constant;
import com.yanq.liancar.util.SwipeRefreshUtil;
import com.yanq.liancar.util.Utils;
import com.yanq.liancar.view.recyclerview.DividerItemDecoration;
import com.yanq.liancar.view.recyclerview.IRecyclerSwipe;
import com.yanq.liancar.view.recyclerview.refresh.OnLoadMoreListener;
import com.yanq.liancar.view.recyclerview.refresh.RecyclerViewWithFooter;

import java.util.List;

public class SystemMessageActivity extends BaseToolbarActivity implements IRecyclerSwipe, SwipeRefreshLayout.OnRefreshListener, OnLoadMoreListener {

    private SwipeRefreshLayout swipeRefresh;
    private RecyclerViewWithFooter swipeRecycler;
    private boolean isRefresh = false;
    private int page = 1;
    private final int NUM = 10;
    private MessageAdapter messageAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusBar(this, true,false);
        setContentView(R.layout.activity_systemmessage);
        mTvTitle.setText("系统消息");

        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        swipeRecycler = (RecyclerViewWithFooter) findViewById(R.id.swipe_recycler);

        SwipeRefreshUtil.setColors(swipeRefresh);

        swipeRecycler.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        messageAdapter = new MessageAdapter(mContext, null);
        messageAdapter.setType(Constant.MSG_SYS);
        swipeRecycler.setAdapter(messageAdapter);
        swipeRecycler.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));

        swipeRefresh.setOnRefreshListener(this);
        swipeRecycler.setOnLoadMoreListener(this);

        loadData();
    }

    private void loadData() {
        mHttpUtil.getMessage(new ProgressSubscriber<MessageModel>(mContext, this) {
            @Override
            public void next(MessageModel model) {
                List<MessageModel.DataBean> data = model.getData();
                if (data != null) {
                    int size = data.size();
                    if (size > 0) {
                        if (messageAdapter != null) {
                            if (isRefresh) {
                                messageAdapter.setData(data);
                            } else {
                                messageAdapter.addData(data);
                            }
                        }
                        if (size < NUM) {
                            setNoMore();
                        }
                    } else {
                        setEmpty();
                    }
                } else {
                    setEmpty();
                }
            }
        }, UserBean.getcToken(mContext), Constant.MSG_SYS, page, NUM);
    }

    private void setEnd() {
        if (messageAdapter != null) {
            messageAdapter.clearData();
            messageAdapter.notifyDataSetChanged();
            swipeRecycler.setEnd("获取数据为空");
        }
    }

    @Override
    public void showItemFail(String msg) {

    }

    @Override
    public void hideSwipeLoading() {
        swipeRefresh.setRefreshing(false);
    }

    @Override
    public void setNoMore() {
        swipeRecycler.setEnd("没有更多数据");
    }

    @Override
    public void setEmpty() {
        swipeRecycler.setEmpty("获取数据为空", R.drawable.empty);
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        loadData();
    }

    @Override
    public void onLoadMore() {
        isRefresh = false;
        page++;
        loadData();
    }
}